<template>
  <el-main>
    <!-- Main content -->
    <el-form :model="searchForm" label-width="80px" :inline="true" size="small">
      <el-form-item prop="collegeName">
        <el-input v-model="searchForm.collegeName" placeholder="请输入学院名称" />
      </el-form-item>
      <el-form-item>
        <el-button icon="el-icon-search" @click="searchBtn">搜索</el-button>
        <el-button style="color:#FF7670;" icon="el-icon-close" @click="resetBtn">重置</el-button>
        <el-button v-permission="['sys:collegeList:add']" type="primary" icon="el-icon-plus" @click="addBtn">新增</el-button>
      </el-form-item>
    </el-form>
    <!-- 新增编辑弹框 -->
    <sys-dialog
      :title="addDialog.title"
      :height="addDialog.height"
      :width="addDialog.width"
      :visible="addDialog.visible"
      @onClose="onclose"
      @onConfirm="onOnConfirm"
    >
      <div slot="content">
        <el-form ref="addModel" :model="addModel" :rules="rules" label-width="80px" :inline="false" size="small">
          <el-form-item prop="collegeName" label="学院名称">
            <el-input v-model="addModel.collegeName" placeholder="请输入学院名称" />
          </el-form-item>
          <el-form-item prop="orderNum" label="学院序号">
            <el-input v-model="addModel.orderNum" placeholder="请输入学院序号" />
          </el-form-item>

        </el-form>

      </div>
    </sys-dialog>
    <!-- 表格 -->
    <el-table :height="tableHeight" :data="tableList" border stripe size="mini">
      <el-table-column prop="collegeName" label="学院名称" />
      <el-table-column prop="orderNum" label="学院序号" />
      <el-table-column v-if="$checkPermission(['sys:collegeList:edit', 'ssys:collegeList:delete'])" label="操作" align="center" width="220">
        <template slot-scope="scope">
          <el-button v-permission="['sys:collegeList:edit']" type="primary" size="small" icon="el-icon-edit" @click="editBtn(scope.row)">编辑</el-button>
          <el-button v-permission="['sys:collegeList:delete']" type="danger" size="small" icon="el-icon-delete" @click="deleteBtn(scope.row)">删除</el-button>
        </template></el-table-column>
    </el-table>
    <!-- 分页 -->
    <el-pagination
      :current-page.sync="searchForm.currentPage"
      :page-sizes="[10, 20, 40, 80, 100]"
      :page-size="searchForm.pageSize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="searchForm.total"
      background
      @size-change="sizeChange"
      @current-change="currentChange"
    />

  </el-main>

</template>

<script>
import { addApi, deleteApi, editApi, listApi } from '@/api/college'
import SysDialog from '@/components/dialog/SysDialog.vue'
export default {
  // 组件注册
  components: {
    SysDialog
  },
  data() {
    return {
      // 表格高度
      tableHeight: 0,
      // 新增表单数据
      addModel: {
        type: '',
        collegeId: '',
        collegeName: '',
        orderNum: ''
      },
      // 新增表单验证规则
      rules: {
        collegeName: [{
          required: true,
          message: '请输入学院名称',
          trigge: 'blur'
        }],
        orderNum: [{
          required: true,
          message: '请输入学院序号'
        }]
      },
      // 新增弹框属性
      addDialog: {
        title: '',
        height: 150,
        width: 650,
        visible: false
      },
      // 搜索表单
      searchForm: {
        collegeName: '',
        currentPage: 1,
        pageSize: 10,
        total: 0
      },
      // 表格数据
      tableList: []
    }
  },
  created() {
    this.getList()
  },
  mounted() {
    this.$nextTick(() => {
      this.tableHeight = window.innerHeight - 220
    })
  },
  methods: {
    // 搜索按钮
    searchBtn() {
      this.getList()
    },
    resetBtn() {
      this.searchForm.collegeName = ''
      this.searchForm.currentPage = 1
      this.getList()
    },
    // 页容量改变时触发
    sizeChange(val) {
      this.searchForm.pageSize = val
      this.getList()
    },
    // 当前页发生变化时触发
    currentChange(val) {
      this.searchForm.currentPage = val
      this.getList()
    },
    // 编辑
    editBtn(row) {
      // 显示弹窗
      this.addDialog.title = '编辑学院'
      // 清空验证与数据
      if (this.$refs.addModel !== undefined) {
        this.$refs.addModel.resetFields()
        this.$emptyData(this.addModel)
      }
      // 回显数据
      this.$objCopy(row, this.addModel)
      this.addModel.type = '1'
      this.addDialog.visible = true
    },
    // 删除
    async deleteBtn(row) {
      // 信息确认
      const confirm = await this.$myconfirm('确认删除该条数据吗？')
      if (confirm) {
        const res = await deleteApi({ collegeId: row.collegeId })
        if (res && res.code === 200) {
          this.$message.success(res.msg)
          // 刷新列表
          this.getList()
        }
      }
    },
    // 获取表格数据
    async getList() {
      const res = await listApi(this.searchForm)
      if (res && res.code === 200) {
        this.tableList = res.data.records
        this.searchForm.total = res.data.total
      }
    },
    addBtn() {
      this.addDialog.title = '新增学院'
      // 清空验证与数据
      if (this.$refs.addModel !== undefined) {
        this.$refs.addModel.resetFields()
        this.$emptyData(this.addModel)
      }
      this.addModel.type = '0'
      this.addDialog.visible = true
    },
    onclose() {
      this.addDialog.visible = false
    },
    onOnConfirm() {
      this.$refs.addModel.validate(async(valid) => {
        console.log(this.addModel)
        if (valid) {
          let res = null
          if (this.addModel.type === '0') {
            res = await addApi(this.addModel)
          } else {
            res = await editApi(this.addModel)
          }
          if (res && res.code === 200) {
            // 信息提示
            this.$message.success(res.msg)
            // 刷新表格
            this.getList()
            this.addDialog.visible = false
          }
        }
      })
    }
  }
}
</script>

<style lang="scss" scoped>

</style>
